#include <cstdio>
#include <cstring>
using namespace std;
#define N 50001
struct Edge {
	int v, nxt;
};
Edge e[(N << 1) + 10];
int e_cnt, heads[N];
inline void add(int u, int v) {
	e[++e_cnt].v = v;
	e[e_cnt].nxt = heads[u];
	heads[u] = e_cnt;
}
struct hpair {
	int first, second;
	friend bool operator<(const hpair &a, const hpair &b) {
		if (a.first == b.first) {
			return a.second > b.second;
		} else {
			return a.first < b.first;
		}
	}
};

int n, k;
bool vis[N];
bool did_vis[N];
bool app[N];
bool did(int cur, int x) {
	if (cur == x) {
		app[cur] = true;
		return true;
	}
	did_vis[cur] = true;
	for (int i = heads[cur]; i; i = e[i].nxt) {
		if (!did_vis[e[i].v])
			if (did(e[i].v, x)) {
				app[cur] = true;
				return true;
			}
	}
	return false;
}
hpair dfs(int x) {
	int v;
	hpair tmp;
	hpair res;
	res.first = 0;
	res.second = x;
	vis[x] = true;
	for (int i = heads[x]; i; i = e[i].nxt) {
		v = e[i].v;
		if (!vis[v]) {
			tmp = dfs(v);
			if (res < tmp) {
				res.first = tmp.first;
				res.second = tmp.second;
			}
		}
	}
	res.first += app[x] ? 0 : 1;
	return res;
}
int main() {
	// freopen("testdata.in", "r", stdin);
	// freopen("testdata.out", "w", stdout);
	scanf("%d%d", &n, &k);
	int t = 0;
	for (int i = 1; i < n; ++i) {
		scanf("%d", &t);
		add(t, i);
		add(i, t);
	}
	hpair opt;
	printf("%d\n", k);
	while (true) {
		memset(did_vis, 0, sizeof(did_vis));
		memset(vis, 0, sizeof(vis));
		opt = dfs(k);
		app[k] = true;
		did(k, opt.second);
		if (opt.first == 0) {
			break;
		}
		printf("%d\n", opt.second);
		k = opt.second;
	}
	// fclose(stdin);
	// fclose(stdout);
	return 0;
}